package com.fromfuture.xuhuidoc.android.Interface;

import com.fromfuture.xuhuidoc.android.commonresouce.MyUrl;

import net.sf.json.JSON;
import net.sf.json.JSONObject;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import app.auto.runner.base.http.OkHttpClientHelper;
import app.auto.runner.base.intf.FunCallback;
import app.auto.runner.base.intf.ListBuilder;
import app.auto.runner.base.intf.MapBuilder;
import app.auto.runner.base.intf.ValueCallback;
import app.auto.runner.base.intf.json.JsonToMapUtils;
import app.auto.runner.base.intf.json.JsonUtil;
import app.auto.runner.base.utility.base.Toastial;


/**
 * Created by hp on 2018/4/20.
 */

public class Interface {
    //change prepart
    static {
        OkHttpClientHelper.setPart(MyUrl.G_WEBBASE);
    }

    public static void addbli(String way, String json, String url, FunCallback funcallback) throws IOException {
        url = "sickblhdr/createSickblhdr";
        String sample = "{\"sick\":[{\"sickguid\":\"2CFCF1E5EDCE6201E050AE0AC684965F\",\"docguid\":\"zcc\",\"doc\":\"丁医生\",\"name\":\"111\",\"sex\":1,\"age\":\"33\",\"ageoptions\":\"岁\",\"cardid\":\"333\",\"medicarecard\":\"111\",\"mainn\":\"333\",\"jws\":\"333\",\"tw\":\"\",\"clucose\":\"\",\"xl\":\"\",\"xy1\":\"\",\"xy2\":\"\",\"weight\":\"\",\"glucose\":\"\",\"clinicaltime\":\"2018-04-23\",\"periodtime\":\"上午\",\"bjfa\":\"333\",\"note\":\"222\"}],\"dts\":[{\"drunam\":\"复方氨基酸（9AA)针\",\"unit\":\"250ml*1毫升\",\"numnit\":\"ml\",\"smlunt\":\"瓶\",\"ypnum\":\"2\",\"pian\":\"一次2片\",\"pc\":\"每日两次\",\"kfwyname\":\"皮下\"}],\"check\":[{\"projectname\":\"\",\"remarks\":\"\"}],\"plan\":[{\"followoption\":\"\",\"followdatetime\":\"\"}],\"diagnosis\":[{\"zhand\":\"\",\"zdname\":\"\"}],\"flag\":\"1\",\"userid\":\"\",\"docid\":\"\"}  ";
        String sampled = sampleText(sample);

        postjson(way, sample, url, funcallback);
    }


    private static String sample(Object sample) {

        if (sample instanceof Map) {
            return sampleMap(((Map) sample));
        } else if (sample instanceof List) {
            return sampleList(((List) sample));
        } else {
            return sample + "";
        }

    }

    private static String sampleText(String sample) {
        Object object = JsonUtil.fromContent(sample);
        return sample(object);
    }


    private static String sampleMap(Map list) {

        String json = JsonToMapUtils.mapToJson(list);
        for (Object o :
                list.keySet()) {
            String sample = sample(list.get(o));

            if (!(list.get(o) instanceof String)) {
                if (sample.contains("#")) {
                    json = json.replace(JsonToMapUtils.objectToJson(list.get(o)).toString(), sample);
                }
            } else {
                String pair = "\"" + o.toString() + "\"" + ":\"" + sample + "\"";
                String replacement = "\"" + o.toString() + "\"" + ":\"#" + o.toString() + "\"";
                json = json.replace(pair, replacement);
            }


        }
        return json;

    }

    private static String sampleList(List list) {
        ListBuilder listBuilder = ListBuilder.build();

        for (Object o : list) {
            listBuilder.add(sample(o));
        }
        return listBuilder.get().toString();

    }


    public static void postjson(String way, String json, String url, FunCallback funcallback) {
        final Map map = app.auto.runner.base.intf.json.JsonUtil.fromContent(json);

        OkHttpClientHelper.call(way,
                url, MapBuilder.withMap(map), new ValueCallback().setIsdefault(false).setFab(funcallback));
    }


    public static void query() {

        String clinichistory = "{\"guid\": \"123\",\"name\": \"\", \"sex\": \"男\",\"birthday\":\"2017-3-6\",\"cardid\":\"123465798\",\"medicalid\":\"36985274\",\"mainn\":\"这是主诉\",\"jws\":\"没有既往史\", \"zhand\":\"这是诊断1,诊断2,诊断3\", \"dts\": [{\"drunam\": \"药品名0\", \"unit\": \"规格0\", \"numnit\": \"数量0\", \"pian\":\"剂量0\", \"pc\":\"频次0\", \"kfwyname\":\"途径0\"},{\"drunam\": \"药品名1\", \"unit\": \"规格1\", \"numnit\": \"数量1\", \"pian\":\"剂量1\", \"pc\":\"频次1\", \"kfwyname\":\"途径1\"}]}";

        Map map = app.auto.runner.base.intf.json.JsonUtil.fromContent(clinichistory);

        OkHttpClientHelper.call("post", "http://106.14.139.5:7014/GZ/dtcasehistory/addcasehistorys", MapBuilder.withMap(map), new ValueCallback().setIsdefault(false).setFab(new FunCallback() {

            @Override
            public void simpleRun(Object aty) {

            }

            @Override
            public void onCallback(Object result, Object object) {
                super.onCallback(result, object);
                Toastial.quick(result.toString());

            }
        }));
    }


    public static void queryClinichistory() {

        String clinichistory = "{\"guid\": \"123\",\"name\": \"\", \"sex\": \"男\",\"birthday\":\"2017-3-6\",\"cardid\":\"123465798\",\"medicalid\":\"36985274\",\"mainn\":\"这是主诉\",\"jws\":\"没有既往史\", \"zhand\":\"这是诊断1,诊断2,诊断3\", \"dts\": [{\"drunam\": \"药品名0\", \"unit\": \"规格0\", \"numnit\": \"数量0\", \"pian\":\"剂量0\", \"pc\":\"频次0\", \"kfwyname\":\"途径0\"},{\"drunam\": \"药品名1\", \"unit\": \"规格1\", \"numnit\": \"数量1\", \"pian\":\"剂量1\", \"pc\":\"频次1\", \"kfwyname\":\"途径1\"}]}";

        Map map = app.auto.runner.base.intf.json.JsonUtil.fromContent(clinichistory);

        OkHttpClientHelper.call("post", "http://106.14.139.5:7014/GZ/dtcasehistory/addcasehistorys", MapBuilder.withMap(map), new ValueCallback().setIsdefault(false).setFab(new FunCallback() {

            @Override
            public void simpleRun(Object aty) {

            }

            @Override
            public void onCallback(Object result, Object object) {
                super.onCallback(result, object);
                Toastial.quick(result.toString());

            }
        }));
    }

    public static void watchbli() {
    }
}
